﻿using Demo.UserService.Entities;
using Demo.UserService.Repositorys;
using Demo.UserService.Services.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.DependencyInjection;
using Mapster;
using Polly;
using Volo.Abp.Domain.Repositories; 


namespace Demo.UserService.Services
{
    /// <summary>
    /// 
    /// </summary>
    [Dependency(ServiceLifetime.Singleton)]
    public class UserService : CrudAppService<ShopUserInfo, UserInfoDto, Guid>, IUserService
    {

        private readonly IUserInfoRepository _userInfoRepository; 

        /// <summary>
        /// 
        /// </summary>
        /// <param name="repository"></param>
        public UserService(IUserInfoRepository repository) : base(repository)
        {
            _userInfoRepository = repository;

        }
 
        /// <summary>
        /// 注册会员
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<UserInfoDto> AddUserAsync(UserInfoDto dto)
        {
            var existsUser = await _userInfoRepository.FindAsync(it => it.UserName == dto.UserName);
            if(existsUser!=null)
            {
                throw new Exception("账号已被注册");
            }

            var userInfo = await _userInfoRepository.AddUserAsync(dto);
            var newUser =   userInfo.Adapt<UserInfoDto>();
            return newUser;

        }

    }
}
